MySQL 表的增删改查 / 数据库约束

您所在的位置:网站首页 create index MySQL 表的增删改查 / 数据库约束

MySQL 表的增删改查 / 数据库约束

#MySQL 表的增删改查 / 数据库约束 | 来源: 网络整理| 查看: 265

MySQL 1 针对数据库进行操作 2 针对数据表进行操作 3 数据的增删改查 3.1 数据的插入 3.2 数据的查找 (重要) 3.3 数据的修改 3.4 数据的删除 4 数据库约束 4.1 约束类型 4.2 null 约束 4.3 unique 唯一约束 4.4 default 默认值约束 4.5 primary key 主键约束 4.6 foreign key 外键约束   数据库,是一类组织管理存储数据的软件,数据存储在磁盘上。而mySQL是一个 C / S 结构的软件,关系型数据库,是以 数据表 的形式来进行组织数据的。  

1 针对数据库进行操作

(1) 创建数据库 create database [数据库名]; (2) 查看数据库 show databases; (3) 删除数据库 drop database [数据库名]; a. MySQL 支持 bin log b. 备份,mysqldump 工具 c. 基于磁盘文件进行还原,删除操作后,数据被标记为无效,如果用工具进行抢救可以找回,但若是创建了新的数据将其覆盖,便无法找回了。 (4) 选中数据库 use [数据库名];

2 针对数据表进行操作

(1) 查看都有什么表 show tables; (2) 创建表 create table [表名] (列); 常用数据类型:int 、decimal、varchar、datetime; (3) 查看表结构 desc [表名]; (4) 删除表 drop table [表名];

3 数据的增删改查 3.1 数据的插入

insert into [表名] (列名) values (值);

3.2 数据的查找 (重要)

(1). 全列查找 select * from [表名];

(2). 指定列查找 select [列名] from [表名];

(3). 带有表达式的查找 select id,name,english + 10 from [表名];

(4). 查找时给指定列起别名 select [列名] as [别名] from [表名];

(5). 查找某一列并且去重 select distinct [列名] from [表名];

(6). 排序:利用 order by 指定某一列进行排序,默认按照升序排序 select name,math from exam_result order by math;加上 desc 就是降序排序 select name,math from exam_result order by math desc;

(7).条件查询  a.基本查询 select name,english from exam_result where english < 60; select name,chinese + math + english from exam_result where chinese + math + english < 200;  b.and 和 or select name,chinese,english from exam_result where chinese > 80 and english > 80; select name,chinese,english from exam_result where chinese > 80 or english > 80;(如果 and 和 or 同时存在,注意and 优先级比 or 高)  c.范围查询 —— between…and… select name,chinese from exam_result where chinese between 80 and 90;  d.范围查询 —— in select name,chinese from exam_result where math in (70,89,90);  e.模糊查找(只要有一部分相同即可,效率较低) select name from exam_result where name like ‘j%’; (找出以 j 开头的字符串) select name from exam_result where name like ‘%j’; (找出以 j 结尾的字符串) select name from exam_result where name like ‘%j%’; (找出包含 j 的字符串) select name from exam_result where name like ‘j_’; (找出 j 开头的并且后面跟着一个字符的符串,每个下划线代表一个字符) select name from exam_result where chinese like ‘8%’; (找出 chinese 值为 80 多的数据)  f.空值查询 select * from exam_result where chinese = math;(不安全的等号,所以结果大概率有误)    select * from exam_result where chinese math;(安全的等号,选择出 chinese值 和 math值 相等的) select * from exam_result where chinese is null;

(8). 分页查询 select * from exam_result limit 3; (查询前三行) select * from exam_result limit 3 offset 0;(查询 0 开始的三条数据) select * from exam_result limit 2,3;(从第 2 条开始查找三条数据)

3.3 数据的修改

update exam_result set chinese = 99 where name = ‘tom’;(将 tom 的 chinese值 改为99) update exam_result set math = math + 10 order by chinese + math + english limit 3;(将总成绩后三名的同学的数学成绩加 10 分)

3.4 数据的删除

delete from exam_result where name = ‘tom’ and id = 10;

4 数据库约束 4.1 约束类型

not null :指某列不能存储空值;unique:保证某列的每行必须有唯一的值;default:规定没有给列赋值时的默认值;primary key:not null 和 unique 的结合,确保某列(或两个列多个列的结合有唯一标识,有助于更容易、更快速地找到表中的一个特定的记录);foreign key:保证一个表中的数据匹配另一个表中的值的参照完整性;

4.2 null 约束

在创建表的时候指定某列不为空: create table student (id int not null,name varchar(50));

4.3 unique 唯一约束

指定某列,要求其每一行都是唯一的、不重复的: create table student (id int not null,name varchar(50) unique);

4.4 default 默认值约束

例如,指定插入数据时,name 列为空,默认值为 unkown: create table student (id int not null,name varchar(50) unique default ‘unkown’);

4.5 primary key 主键约束

设置 id 为主键,主键是 not null 和 unique 的结合,是一条记录的唯一身份标识,一个表中只能有一个主键: create table student (id int primary key,name varchar(50) unique);

对于整数类型的 主键,搭配 auto_increment 来使用,插入数据对应字段不给定值时,使用最大值 + 1。 create table student (id int primary key auto_increment,name varchar(50) unique);

4.6 foreign key 外键约束

把上面的学生表删除,我们再来创建一个班级表 class ,id 为主键 : create table class (id int primary key ,name varchar(50)); create table student (id int primary key auto_increment,name varchar(50),classId int,foreign key (classId) references class(id));

外键用于关联其他表的主键或唯一键: foreign key (classId) references class(id) ,其中对当前表的classId 这一列进行约束,class(id) 和另外一个 class 表建立其中的 id 列关联起来。(外键约束就是要求当前表里面的 classId 字段的值,必须在 class 表的 id 中出现过才可以,并且注意:id 必须是 class 表的主键)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3